package com.wwwarehouse.taskcenter.fragment.job_point.step7;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.JobPointSetRuleResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.constant.job_point.TaskCenterCommon;
import com.wwwarehouse.taskcenter.eventbus_event.JobPointRuleEvent;
import com.wwwarehouse.taskcenter.eventbus_event.JobPointStateEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_BUSINESS_ID;
import static com.wwwarehouse.taskcenter.fragment.job_point.step7.SetJobPointRuleFragment.KEY_SET_JOB_POINT_LIST;

/**
 * Created by Jackie on 2017/11/13.
 * 选择作业点规则
 */

public class ChooseJobPointRuleFragment extends CommonBasePagerFragment implements View.OnClickListener {
    private ArrayList<JobPointSetRuleResponseBean.JobPointRuleDetailsBean> mJobPointRuleDetailsList;

    private String mBusinessId;

    private Button mAddBtn;
    private Button mSureBtn;

    private static final int REQUEST_GET_RULE_INFO = 0;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    private void initView() {
        mJobPointRuleDetailsList = new ArrayList<>();
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            if (TaskCenterCommon.getInstance().isDetails()) {
                mJobPointRuleDetailsList = bundle.getParcelableArrayList(KEY_SET_JOB_POINT_LIST);
            } else {
                mBusinessId = bundle.getString(KEY_BUSINESS_ID);
            }
        }

        mAddBtn = new Button(mActivity);
        mSureBtn = new Button(mActivity);
    }

    private void initEvent() {
        mAddBtn.setOnClickListener(this);
        mSureBtn.setOnClickListener(this);

        EventBus.getDefault().register(this);
    }

    @Override
    public void onClick(View v) {
        if (v == mAddBtn) {
            //添加规则
            Bundle bundle = new Bundle();
            bundle.putString(KEY_BUSINESS_ID, mBusinessId);

            ChooseAddWayRuleTemplateFragment chooseAddWayRuleTemplateFragment = new ChooseAddWayRuleTemplateFragment();
            chooseAddWayRuleTemplateFragment.setArguments(bundle);
            pushFragment(chooseAddWayRuleTemplateFragment);
        } else if (v == mSureBtn) {
            //确定
            popFragment();

            Intent intent = new Intent("ChooseJobPointRule");

            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList(KEY_SET_JOB_POINT_LIST, mJobPointRuleDetailsList);
            intent.putExtras(bundle);

            mActivity.sendBroadcast(intent);
        }
    }

    @Override
    public void onDownPull() {

    }

    @Override
    public void onRefreshListener() {
        getRuleInfo();
    }

    @Override
    public void onLoadSuccess(String data, int what) {
        switch (what) {
            case REQUEST_GET_RULE_INFO:
                JobPointSetRuleResponseBean jobPointSetRuleResponseBean = JSON.parseObject(data, JobPointSetRuleResponseBean.class);
                ArrayList<JobPointSetRuleResponseBean.JobPointRuleDetailsBean> jobPointRuleDetailsList = (ArrayList<JobPointSetRuleResponseBean.JobPointRuleDetailsBean>) jobPointSetRuleResponseBean.getList();

                try {
                    Bundle bundle = new Bundle();
                    bundle.putString(KEY_BUSINESS_ID, mBusinessId);
                    setData(jobPointSetRuleResponseBean.getTotal(), 10, new ChooseJobPointRuleDetailsFragment().getClass().getName(), bundle, jobPointRuleDetailsList);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                showConfirmButton();

                RelativeLayout relativeLayout = getConfirmLayout();
                relativeLayout.removeAllViews();

                //新增规则
                RelativeLayout.LayoutParams addParams = new RelativeLayout.LayoutParams(ConvertUtils.dip2px(mActivity, 55), ConvertUtils.dip2px(mActivity, 55));
                addParams.setMargins(0, 0, 0, 10);
                mAddBtn.setLayoutParams(addParams);
                mAddBtn.setBackground(getResources().getDrawable(R.drawable.cir_btn_add_blue_selector));
                mAddBtn.setText(getString(R.string.add_new_rule));
                mAddBtn.setTextColor(getResources().getColor(R.color.common_color_c3_ffffff));
                mAddBtn.setTextSize(14);

                //确定
                RelativeLayout.LayoutParams confirmParams = new RelativeLayout.LayoutParams(ConvertUtils.dip2px(mActivity, 55), ConvertUtils.dip2px(mActivity, 55));
                confirmParams.setMargins(ConvertUtils.dip2px(mActivity, 100), 0, 0, 10);
                mSureBtn.setLayoutParams(confirmParams);
                mSureBtn.setBackground(getResources().getDrawable(R.drawable.cir_btn_gray_blue_selector));
                mSureBtn.setText(getString(R.string.confirm));
                mSureBtn.setTextColor(getResources().getColor(R.color.common_color_c3_ffffff));
                mSureBtn.setTextSize(14);

                mSureBtn.setEnabled(false);

                relativeLayout.addView(mAddBtn);
                relativeLayout.addView(mSureBtn);
                break;
            default:
                break;
        }
    }

    @Override
    public void onLoadError(String errorInfo) {
        toast(errorInfo);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseJobPointRuleFragment) {
            mActivity.setTitle(R.string.choose_job_point_rule);
        }
    }

    @Override
    public void requestDatas() {
        super.requestDatas();

        if (TaskCenterCommon.getInstance().isDetails()) {
            try {
                Bundle bundle = new Bundle();

                setData(mJobPointRuleDetailsList.size(), 10, new ChooseJobPointRuleDetailsFragment().getClass().getName(), bundle, mJobPointRuleDetailsList);
            } catch (Exception e) {
                e.printStackTrace();
            }

            //有结果才显示确定按钮
            if (mJobPointRuleDetailsList.size() > 0) {
                initConfirmBtn();
            }
        } else {
            getRuleInfo();
        }
    }

    public void onEventMainThread(JobPointStateEvent event) {
        if (peekFragment() instanceof ChooseJobPointRuleFragment) {
            if (TaskCenterCommon.getInstance().isDetails()) {
                for (JobPointSetRuleResponseBean.JobPointRuleDetailsBean jobPointRuleDetailsBean : mJobPointRuleDetailsList) {
                    if (jobPointRuleDetailsBean.isSelect()) {
                        mConfirmBtn.setEnabled(true);

                        return;
                    }
                }

                mConfirmBtn.setEnabled(false);
            } else {
                if (event.getSelectObject() instanceof JobPointSetRuleResponseBean.JobPointRuleDetailsBean) {
                    JobPointSetRuleResponseBean.JobPointRuleDetailsBean jobPointRuleDetailsBean = (JobPointSetRuleResponseBean.JobPointRuleDetailsBean) event.getSelectObject();

                    if (jobPointRuleDetailsBean.isSelect()) {
                        mJobPointRuleDetailsList.add(jobPointRuleDetailsBean);
                    } else {
                        mJobPointRuleDetailsList.remove(jobPointRuleDetailsBean);
                    }
                }

                if (mJobPointRuleDetailsList.size() > 0) {
                    mSureBtn.setEnabled(true);
                } else {
                    mSureBtn.setEnabled(false);
                }
            }
        }
    }

    public void onEventMainThread(JobPointRuleEvent event) {
        if (peekFragment() instanceof ChooseJobPointRuleFragment) {
            mStateLayout.showProgressView(false);

            getRuleInfo();
        }
    }

    /**
     * 查询已设置的作业点规则
     */
    private void getRuleInfo() {
        Map map = new HashMap();

        map.put("buId", mBusinessId);
        map.put("page", 1);
        map.put("size", 10);

        loadData(TaskCenterConstant.URL_GET_RULE_INFO, map, REQUEST_GET_RULE_INFO);
    }

    private void initConfirmBtn() {
        showConfirmButton();

        setConfirmBtnText(mActivity.getString(R.string.confirm));

        mConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popFragment();

                Intent intent = new Intent("ChooseJobPointRule");

                ArrayList <JobPointSetRuleResponseBean.JobPointRuleDetailsBean> jobPointRuleDetailsList = new ArrayList<>();
                for (JobPointSetRuleResponseBean.JobPointRuleDetailsBean jobPointRuleDetailsBean : mJobPointRuleDetailsList) {
                    if (jobPointRuleDetailsBean.isSelect()) {
                        jobPointRuleDetailsList.add(jobPointRuleDetailsBean);
                    }
                }

                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList(KEY_SET_JOB_POINT_LIST, jobPointRuleDetailsList);
                intent.putExtras(bundle);

                mActivity.sendBroadcast(intent);
            }
        });
    }
}
